package com.tanhua.dubbo.api;

import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.domain.mongodb.FocusUser;
import com.tanhua.model.domain.mongodb.Video;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author maorf
 * @version 1.0
 */
@DubboService
public class VideoApiImpl implements VideoApi{
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdWorker idWorker;
    @Override
    public PageResult getSmallVideos(Integer page, Integer pagesize, Long userId) {
        String key = redisTemplate.opsForValue().get("VIDEOS_RECOMMEND_" + userId);
        //没有值,随机推荐5条
        if (StringUtils.isEmpty(key)){
            //随机推荐5条
            TypedAggregation aggregation = Aggregation.newAggregation(Video.class,
                    Aggregation.sample(pagesize));

            AggregationResults<Video> videos = mongoTemplate.aggregate(aggregation,Video.class);

            List<Video> mappedResults = videos.getMappedResults();
            List<Video> collect = mappedResults.stream().filter((item) -> {
                return item.getUserId() != userId;
            }).collect(Collectors.toList());
            return new PageResult(page,pagesize,collect.size(),collect);
        }else{
            String[] split = key.split(",");
            List<Long> list = new ArrayList<>();
            for (String s : split) {
                list.add(Long.valueOf(s));
            }
            Pageable pageable = PageRequest.of(page-1,pagesize);
            Query query = Query.query(Criteria.where("vid").in(list)).with(pageable);
            List<Video> videos = mongoTemplate.find(query, Video.class);
            return new PageResult(page,pagesize,videos.size(),videos);
        }
    }

    //保存
    public void save(Video video) {
        video.setId(ObjectId.get());
        video.setCreated(System.currentTimeMillis());
        video.setVid(idWorker.getNextId("video"));
        mongoTemplate.save(video);
    }

    @Override
    public void saveFocusUser(FocusUser followUser) {
        followUser.setId(ObjectId.get());
        followUser.setCreated(System.currentTimeMillis());
        mongoTemplate.save(followUser);
    }

    @Override
    public void deleteFollowUser(Long userId, Long followUserId) {
        Query query = Query.query(Criteria.where("followUserId").is(followUserId));
        mongoTemplate.remove(query,FocusUser.class);
    }
}
